from geoalchemy2.types import Geometry
from sqlalchemy import Column
from sqlalchemy.dialects import postgresql

from db.db import db  #第一个db为包名，第二个db为模块名，第三个为数据库实例名


# 关系-对象映射
class GPSTable(db.Model):
    __tablename__ = 'gps_20161101'

    id = Column(postgresql.INTEGER, primary_key=True)
    id_driver = Column(postgresql.TEXT)
    id_order = Column(postgresql.TEXT)


class OrderTable(db.Model):
    __tablename__ = 'order_20161101'

    id = Column(postgresql.TEXT, primary_key=True)
    id_driver = Column(postgresql.TEXT)
    id_state_start = Column(postgresql.INTEGER)
    id_state_end = Column(postgresql.INTEGER)
    reward = Column(postgresql.FLOAT)

    __mapper_args__ = {"order_by": id}

    def __init__(self, id):
        self.id = id

    def __repr__(self):
        return '<Order(id=%r)>' % self.id


class DriverTable(db.Model):
    id = Column(postgresql.TEXT, primary_key=True)
    count_order = Column(postgresql.INTEGER)
    sum_reward = Column(postgresql.NUMERIC)
    avg_order_reward = Column(postgresql.NUMERIC)
    avg_minute_reward = Column(postgresql.NUMERIC)

    def __init__(self, id):
        self.id = id

    def __repr__(self):
        return '<Driver(id = %r)>' % self.id
    
    def set_tablename(self,tablename):
        self.__tablename__ = tablename


class StatesTable(db.Model):
    __tablename__ = 'state'

    gid = Column(postgresql.INTEGER, primary_key=True)
    row = Column(postgresql.INTEGER)
    column = Column(postgresql.INTEGER)
    value = Column(postgresql.FLOAT)
    delta = Column(postgresql.FLOAT)

    def __repr__(self):
        return '<state %r>' % self.gid


class StateStatisticTable(db.Model):
    __tablename__ = 'state_20161101'

    gid = Column(postgresql.INTEGER, primary_key=True)
    row = Column(postgresql.INTEGER)
    column = Column(postgresql.INTEGER)
    start_count = Column(postgresql.INTEGER)
    end_count = Column(postgresql.INTEGER)
    

class TDTable(db.Model):
    __tablename__ = 'tdlearning'

    gid = Column(postgresql.INTEGER, primary_key=True)
    row = Column(postgresql.INTEGER)
    column = Column(postgresql.INTEGER)
    value = Column(postgresql.FLOAT)
    delta = Column(postgresql.FLOAT)
    count = Column(postgresql.FLOAT)

    def __repr__(self):
        return '<state %r>' % self.gid


class TimesTable(db.Model):
    """时间表"""

    __tablename__ = 'times'

    id = Column(postgresql.INTEGER, primary_key=True)
    time_start = Column(postgresql.TIME)
    time_end = Column(postgresql.TIME)

    def __init__(self, id, time_start, time_end):
        self.id = id
        self.time_start = time_start
        self.time_end = time_end


class DriversServiceTable(db.Model):
    __tablename__ = 'drivers_service'

    id = Column(postgresql.INTEGER, primary_key=True)
    time_start = Column(postgresql.TIME)
    time_end = Column(postgresql.TIME)
    states_id = Column(postgresql.INTEGER)
    drivers_id = Column(postgresql.TEXT)
    driver_state = Column(postgresql.CHAR)

    def __init__(self, time_start, time_end, states_id, drivers_id):
        self.time_start = time_start
        self.time_end = time_end
        self.states_id = states_id
        self.drivers_id = drivers_id


class SimulationTable(db.Model):
    __tablename__ = 'simulation'

    id = Column(postgresql.TEXT, primary_key=True)
    time_cost = Column(postgresql.INTERVAL)
    drivers_id = Column(postgresql.TEXT)
    reward = Column(postgresql.FLOAT)

    __mapper_args__ = {"order_by": id}

    def __init__(self, id, time_cost, drivers_id, reward):
        self.id = id
        self.time_cost = time_cost
        self.drivers_id = drivers_id
        self.reward = reward

    def __repr__(self):
        return '<Order(id=%r)>' % self.id



